﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace SP2010PreRequisites_Installer
{
    public partial class frmMain : Form
    {
        Helper installerHelper = new Helper();
        PreRequisite[] allPreRequisites;

        public frmMain()
        {
            InitializeComponent();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            allPreRequisites = installerHelper.GetPreRequisitesList();

            if (allPreRequisites.Length > 0)
            {
                lstPreRequisites.Items.Clear();

                foreach (var preRequisite in allPreRequisites)
                {
                    lstPreRequisites.Items.Add(preRequisite.Title);
                }
            }
        }

        private void btnInstall_Click(object sender, EventArgs e)
        {
            lstPreRequisites.Enabled = false;
            txtMessage.Clear();

            for (int itemIndex = 0; itemIndex < allPreRequisites.Length; itemIndex++)
            {
                lstPreRequisites.SelectedIndex = itemIndex;

                string preRequisiteFolderPath = Path.Combine(Directory.GetCurrentDirectory(), "Data");
                allPreRequisites[itemIndex].LocalFilePath = Path.Combine(preRequisiteFolderPath, allPreRequisites[itemIndex].FileName);

                ShowMessage ("Downloading file (" + allPreRequisites[itemIndex].FileName + "): " + (itemIndex + 1) + " of " + (allPreRequisites.Length - 1));
                var downloadFileTask = Task.Run(() => installerHelper.DownloadFile(allPreRequisites[itemIndex].Path, allPreRequisites[itemIndex].LocalFilePath));
                downloadFileTask.Wait();

                if (installerHelper.LastException.Equals(string.Empty))
                {
                    ShowMessage("Downloaded succesfully.");

                    allPreRequisites[itemIndex].FailedToDownload = false;
                }
                else
                {
                    if (installerHelper.LastException.Equals(Helper.FILE_EXISTS))
                    {
                        ShowMessage(Helper.FILE_EXISTS);

                        allPreRequisites[itemIndex].FailedToDownload = true;
                    }
                    else
                    {
                        ShowMessage("Downloaded Failed. Server error: " + installerHelper.LastException);
                        installerHelper.LastException = string.Empty;

                        allPreRequisites[itemIndex].FailedToDownload = true;
                    }
                }
            }

            if (MessageBox.Show (this, "Do you want to continue and install the downloaded Pre-Requisites?", "Confirm Installation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK)
            {
                /*
             * Installation of downloaded files.
             */

                ShowMessage("");
                ShowMessage("");
                ShowMessage("**** Installing the downloaded pre-requsites");

                for (int itemIndex = 0; itemIndex < allPreRequisites.Length; itemIndex++)
                {
                    lstPreRequisites.SelectedIndex = itemIndex;

                    if (MessageBox.Show(this, "Do you want to install (" + allPreRequisites[itemIndex].FileName + ") ?", "Confirm Installation", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.OK)
                    {
                        ShowMessage("Installing (" + allPreRequisites[itemIndex].FileName + "): " + (itemIndex + 1) + " of " + (allPreRequisites.Length - 1));
                        var installFileTask = Task.Run(() => installerHelper.Install(allPreRequisites[itemIndex].LocalFilePath, string.Empty));
                        installFileTask.Wait();

                        if (installerHelper.LastException.Equals(string.Empty))
                        {
                            allPreRequisites[itemIndex].FailedToInstall = false;

                            ShowMessage("Installed successfully.");
                        }
                        else
                        {
                            allPreRequisites[itemIndex].FailedToInstall = true;

                            ShowMessage(installerHelper.LastException + "(" + allPreRequisites[itemIndex].FileName + ").");
                        }
                    }
                }

                ShowMessage("");
                ShowMessage("");
                ShowMessage("**** Process finished.");
            }

            lstPreRequisites.Enabled = true;
        }

        private void ShowMessage (string Message)
        {
            txtMessage.Text += Message + Environment.NewLine;

            txtMessage.ScrollToCaret();
        }
    }
}
